package com.jgsu.farm.mapper.sql;

import com.jgsu.farm.entity.DiseaseStress;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DiseaseStressMapperSql {
    @Insert("insert into diseasestress(stressType,cropID,chineseName,latinName,alias,distribution,symptom,pathogeny,occurrence,control,pathoImage,symptomImage) values(#{stresstype},#{cropid},#{chinesename},#{latinname},#{alias},#{distribution},#{symptom},#{pathogeny},#{occurrence},#{control},#{pathoimage},#{symptomimage})")
    @Options(useGeneratedKeys = true,keyProperty = "diseaseid",keyColumn = "diseaseID")
    Integer add(DiseaseStress diseasestress);

    @Select("select * from diseasestress where chineseName = #{chinesename}")
    Integer selectByName(DiseaseStress diseasestress);


    @Select("select * from diseasestress where diseaseID = #{diseaseid}")
    Integer selectById(Integer diseaseid);

    @Delete("delete from diseasestress where diseaseID = #{diseaseid}")
    void delete(Integer diseaseid);

    @Results(id = "diseaseStressMap", value = {
            @Result(id = true, column = "diseaseid", property = "diseaseid"),
            @Result(column = "stresstype", property = "stresstype"),
            @Result(column = "stresstype",property = "stressCategory",one = @One(select = "com.jgsu.farm.mapper.sql.StressCategoryMapperSql.selectStressCategoryById",fetchType = FetchType.EAGER)),
            @Result(column = "cropid", property = "cropid"),
            @Result(column = "cropid",property = "plant",one = @One(select = "com.jgsu.farm.mapper.sql.PlantMapperSql.selectPlantById",fetchType = FetchType.EAGER)),
            @Result(column = "chinesename", property = "chinesename"),
            @Result(column = "latinname", property = "latinname"),
            @Result(column = "alias", property = "alias"),
            @Result(column = "distribution", property = "distribution"),
            @Result(column = "symptom", property = "symptom"),
            @Result(column = "pathogeny", property = "pathogeny"),
            @Result(column = "occurrence", property = "occurrence"),
            @Result(column = "control", property = "control"),
            @Result(column = "pathoimage", property = "pathoimage"),
            @Result(column = "pathoimage",property = "matchPathImage",many = @Many(select = "com.jgsu.farm.mapper.sql.MatchImageMapperSql.findMatchImageByPlantId",fetchType = FetchType.LAZY)),
            @Result(column = "symptomimage", property = "symptomimage"),
            @Result(column = "symptomimage",property = "matchSymptomImage",many = @Many(select = "com.jgsu.farm.mapper.sql.MatchImageMapperSql.findMatchImageByPlantId",fetchType = FetchType.LAZY))
    })
    @Select("select * from diseasestress")
    List<DiseaseStress> selectAll();

    @Update("update diseasestress set stressType = #{stresstype},cropID = #{cropid},chineseName = #{chinesename},latinName = #{latinname},alias = #{alias},distribution = #{distribution},symptom = #{symptom},pathogeny = #{pathogeny},occurrence = #{occurrence},control = #{control},pathoImage = #{pathoimage},symptomImage = #{symptomimage} where diseaseID = #{diseaseid}")
    Integer modify(DiseaseStress diseasestress);
}
